package org.jeecg.modules.demo.warehouse.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.modules.demo.warehouse.mapper.WmsWarehousesMapper;
import org.jeecg.modules.demo.warehouse.service.IWmsWarehousesService;
import org.jeecg.modules.wms.config.WarehouseDictEnum;
import org.jeecg.modules.wms.warehouse.entity.WmsWarehouses;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description: 仓库表
 * @Author: jeecg-boot
 * @Date:   2025-10-25
 * @Version: V1.0
 */
@Service
public class WmsWarehousesServiceImpl extends ServiceImpl<WmsWarehousesMapper, WmsWarehouses> implements IWmsWarehousesService {

    @Override
    public void add(WmsWarehouses wmsWarehouses) {
        //1、根据仓库代码查询仓库信息
        long count = this.count(Wrappers.lambdaQuery(WmsWarehouses.class).eq(WmsWarehouses::getWarehouseCode, wmsWarehouses.getWarehouseCode()));
        //2、存在 则抛出异常告诉前端仓库代码已存在
        if (count > 0) {
            throw new RuntimeException("仓库代码已存在");
        }
        //3、不存在 则保存
        this.save(wmsWarehouses);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(WmsWarehouses wmsWarehouses) {
        //1、状态为启用的时候不能修改仓库的，仓库代码不能重复！
        //根据仓库代码查询仓库信息
        // select count(*) where warehouse_code=? and id != ?
        long count = this.count(Wrappers.lambdaQuery(WmsWarehouses.class)
                .eq(WmsWarehouses::getWarehouseCode, wmsWarehouses.getWarehouseCode())
                .ne(WmsWarehouses::getId, wmsWarehouses.getId()));
        if (count > 0) {
            throw new RuntimeException("仓库已经存在");
        }
        WmsWarehouses warehouses = this.getOne(Wrappers.lambdaQuery(WmsWarehouses.class).eq(WmsWarehouses::getId, wmsWarehouses.getId()));
        if (WarehouseDictEnum.STATUS_ACTIVE.getCode().equals(warehouses.getStatus())) {
            throw new RuntimeException("仓库状态异常不能被修改");
        }
        this.updateById(wmsWarehouses);
    }

    @Override
    public void delById(String id) {
        //1、根据id查询仓库信息
        WmsWarehouses wmsWarehouses = this.getOne(Wrappers.lambdaQuery(WmsWarehouses.class).eq(WmsWarehouses::getId, id));
        //2、校验仓库状态
        if (!WarehouseDictEnum.STATUS_CREATED.getCode().equals(wmsWarehouses.getStatus())) {
            throw new RuntimeException("仓库异常,不能被删除");
        }
        this.removeById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void enable(String id) {
        //1、参数校验
        boolean empty = ObjectUtil.isEmpty(id);
        if (empty) {
            throw new RuntimeException("参数不能为空");
        }
        //2、根据id查询仓库信息
        WmsWarehouses wmsWarehouses = this.getOne(Wrappers.lambdaQuery(WmsWarehouses.class).eq(WmsWarehouses::getId, id));
        if (ObjectUtil.isNull(wmsWarehouses)) {
            throw new RuntimeException("仓库不存在");
        }
        //3、校验仓库状态
        if (!WarehouseDictEnum.STATUS_CREATED.getCode().equals(wmsWarehouses.getStatus())
                && !WarehouseDictEnum.STATUS_INACTIVE.getCode().equals(wmsWarehouses.getStatus())) {
            throw new RuntimeException("仓库状态异常,不能被启用");
        }
        // 4、启用
        wmsWarehouses.setStatus(WarehouseDictEnum.STATUS_ACTIVE.getCode());
        this.updateById(wmsWarehouses);
    }

    @Override
    public void disable(String id) {
        //1、参数校验
        boolean empty = ObjectUtil.isEmpty(id);
        if (empty) {
            throw new RuntimeException("参数不能为空");
        }
        //2、根据id查询仓库信息
        WmsWarehouses wmsWarehouses = this.getOne(Wrappers.lambdaQuery(WmsWarehouses.class).eq(WmsWarehouses::getId, id));
        if (ObjectUtil.isNull(wmsWarehouses)) {
            throw new RuntimeException("仓库不存在");
        }
        //3、校验仓库状态
        if (!WarehouseDictEnum.STATUS_ACTIVE.getCode().equals(wmsWarehouses.getStatus())) {
            throw new RuntimeException("仓库状态异常,不能被禁用");
        }
        // 4、禁用
        wmsWarehouses.setStatus(WarehouseDictEnum.STATUS_INACTIVE.getCode());
        this.updateById(wmsWarehouses);
    }
}
